﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASBCC.Workflow.K2Manager;
using ASBCC.Database.Entity;

namespace ASBCC.Workflow.Client
{
    public class EZVisitWorkflowController : WorkflowControllerBase
    {
        private IAppForm _appform;
        public IAppForm Appform
        {
            get
            {
                return _appform;
            }
        }
        public EZVisitWorkflowController(IWorkflowPage workflowPage, IAppForm appForm, MyActivityTableEntity myActivity)
            : base(workflowPage, appForm, myActivity)
        {
            _appform = appForm;

        }
        public override void BuildAuditDestinationArgument(string actionType)
        {
            string auditDestination = string.Empty;
            string[] nextActivityNameList = GetNextActivityNameList();
            foreach (string nextActivityName in nextActivityNameList)
            {
                Flow_ProcessConfigTableEntity processConfig = K2DataHelper.GetProcessConfig(GetProcName(), nextActivityName);
                if (processConfig.AuditUserSource != "Customization")
                {
                    base.BuildAuditDestinationArgument(actionType);
                    return;

                }

                Dictionary<string, object> formArguments = GetFormArguments();
                int[] destination;
                switch (nextActivityName)
                {
                    case EZVisitActivityNameConsts.FNC_Controller:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.FNC_Controller]);
                        break;
                    case EZVisitActivityNameConsts.Dept_Head:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.Dept_Head]);
                        break;
                    case EZVisitActivityNameConsts.CEO_Delegation:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.CEO_Delegation]);
                        break;
                    case EZVisitActivityNameConsts.COD_PP_Head:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.COD_PP_Head]);
                        break;
                    case EZVisitActivityNameConsts.LGD_Check_Person:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.LGD_Check_Person]);
                        break;
                    case EZVisitActivityNameConsts.LGD:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.LGD]);
                        break;
                    case EZVisitActivityNameConsts.Post_Submission:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.Post_Submission]);
                        break;
                    case EZVisitActivityNameConsts.Post_COD_PP_Check_Person:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.Post_COD_PP_Check_Person]);
                        break;
                    case EZVisitActivityNameConsts.Post_Dept_Head:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.Post_Dept_Head]);
                        break;
                    case EZVisitActivityNameConsts.Post_COD_PP_Manager:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.Post_COD_PP_Manager]);
                        break;
                    case EZVisitActivityNameConsts.Post_COD_PP_Head:
                        destination = (int[])(formArguments[EZVisitActivityNameConsts.Post_COD_PP_Head]);
                        break;
                    case EZVisitActivityNameConsts.COD_PP_Check_Person:
                    case EZVisitActivityNameConsts.COD_PP_Manager:
                        return;
                    default:
                        base.BuildAuditDestinationArgument(actionType);
                        return;
                }
                if (destination == null || destination.Length == 0)
                {
                    throw new Exception(" AuditDestination  is null!");
                }
                K2AuditDestination k2AuditDestination = new K2AuditDestination(nextActivityName, destination);
                auditDestination = k2AuditDestination.ExportText();
                formArguments[WorkflowKeyConsts.AuditDestination] = auditDestination;
            }
        }

        public override void BuildK2DataFields(Dictionary<string, object> dataFields)
        {
            base.BuildK2DataFields(dataFields);
            Dictionary<string, object> formArguments = GetFormArguments();
            string CODPPCheckPerson = formArguments[EZVisitWorkflowKeyConsts.CODPPCheckPerson].ToString();
            string PDC = formArguments[EZVisitWorkflowKeyConsts.PDC].ToString();
            string CODPPManager = formArguments[EZVisitWorkflowKeyConsts.CODPPManager].ToString();


            dataFields[EZVisitWorkflowKeyConsts.CODPPCheckPerson] = CODPPCheckPerson;
            dataFields[EZVisitWorkflowKeyConsts.PDC] = PDC;
            dataFields[EZVisitWorkflowKeyConsts.CODPPManager] = CODPPManager;

        }

        public string[] GetNextActivityNameList()
        {
            List<string> list = new List<string>();


            ActivityLink activityLink = GetActivityLink();
            if (activityLink == null) return null;
            string currentActivityName = string.Empty;
            currentActivityName = GetCurrentActivityName();
            if ((IsDraft() || IsSubmitted()) && string.IsNullOrEmpty(currentActivityName))
            {
                currentActivityName = GetSubmissionActivityName();
            }
            //else
            //{
            //    currentActivityName = GetCurrentActivityName();
            //}
            switch (currentActivityName)
            {
                case EZVisitActivityNameConsts.Dept_Head:
                    list.Add(EZVisitActivityNameConsts.COD_PP_Check_Person);
                    list.Add(EZVisitActivityNameConsts.PDC);
                    break;
                case EZVisitActivityNameConsts.COD_PP_Check_Person:
                    list.Add(EZVisitActivityNameConsts.COD_PP_Manager);
                    break;
                case EZVisitActivityNameConsts.PDC:
                    list.Add(EZVisitActivityNameConsts.COD_PP_Manager);
                    break;
                case EZVisitActivityNameConsts.COD_PP_Manager:
                    list.Add(EZVisitActivityNameConsts.CEO_Delegation);
                    break;
                default:
                    ActivityLinkNode currentNode = activityLink.FindNode(currentActivityName);
                    if (currentNode != null)
                    {
                        ActivityLinkNode lastChild = currentNode.LastChild;
                        if (lastChild != null)
                            list.Add(lastChild.Value);
                    }
                    break;
            }

            return list.ToArray();

            //return base.GetNextActivityName();
        }

        public override string GetNextActivityName()
        {
            return GetNextActivityNameList()[0];
            //return base.GetNextActivityName();
        }

        public override void InitActionButtonClientClick(System.Web.UI.WebControls.Button actionButton)
        {
            base.InitActionButtonClientClick(actionButton);

            //Dictionary<string, object> formArguments = GetFormArguments();
            //string actionType = WorkflowUtility.GetActionType(actionButton);
            //IAppForm appForm = GetAppForm();
            //bool CurrentUserIsProjectCPM = (bool)formArguments[EZVisitWorkflowKeyConsts.CurrentUserIsProjectCPM];

            //if (appForm.ProcessStatus == ProcessStatusConsts.End && actionType == K2ActionType.Save)
            //{
            //    if (CurrentUserIsProjectCPM)
            //    {
            //        actionButton.OnClientClick = "if (!CheckApproved()) return; var res = __wf_onActionClientClick(); if (!res) return";
            //    }
            //}

        }

        public override void InitActionButtonVisibility(System.Web.UI.WebControls.Button actionButton)
        {
            //Dictionary<string, object> formArguments = GetFormArguments();
            //bool CurrentUserIsProjectCPM = (bool)formArguments[EZVisitWorkflowKeyConsts.CurrentUserIsProjectCPM];
            //string actionType = WorkflowUtility.GetActionType(actionButton);
            //IAppForm appForm = GetAppForm();

            //if (appForm.ProcessStatus == ProcessStatusConsts.End && actionType == K2ActionType.Save)
            //{
            //    if (CurrentUserIsProjectCPM)
            //    {
            //        actionButton.Visible = true;
            //    }
            //    else
            //        actionButton.Visible = false;
            //}
            //else
            //{
            //    actionButton.Visible = false;
            //}
            base.InitActionButtonVisibility(actionButton);
        }

        public override ActivityLink GetActivityLink()
        {
            Dictionary<string, object> formArguments = GetFormArguments();
            //bool bProductLineCPM_Equals_ProjectCPM = (bool)formArguments[BudgetWorkflowKeyConsts.ProductLineCPM_Equals_ProjectCPM];
            //bool bProjectCPMLeader_Equals_PMDirector = (bool)formArguments[BudgetWorkflowKeyConsts.ProjectCPMLeader_Equals_PMDirector];

            string strApplicant = formArguments[EZVisitWorkflowKeyConsts.Applicant].ToString();
            //string strProductLineCPM = formArguments[EZVisitWorkflowKeyConsts.ProductLineCPM].ToString();
            //string strProjectCPM = formArguments[EZVisitWorkflowKeyConsts.ProjectCPM].ToString();
            //string strProjectCPMLeader = formArguments[EZVisitWorkflowKeyConsts.ProjectCPMLeader].ToString();
            //string strPMDirector = formArguments[EZVisitWorkflowKeyConsts.PMDirector].ToString();

            string[] activityNameArr = null;

            //if (strProjectCPM == strPMDirector ||
            //    strProjectCPMLeader == strApplicant || strProjectCPMLeader == strProductLineCPM ||
            //    strPMDirector == strApplicant || strPMDirector == strProductLineCPM ||
            //    ((strProjectCPM == strApplicant || strProjectCPM == strProductLineCPM) && strProjectCPMLeader == strPMDirector)
            //    )
            //{
            //    activityNameArr = new string[] {
            //            ActivityNameConsts.Submission,
            //            EZVisitActivityNameConsts.Product_Line_CPM,
            //            EZVisitActivityNameConsts.PM_Director,
            //            ActivityNameConsts.End
            //        };
            //}
            //else if ((strProjectCPM == strApplicant || strProjectCPM == strProductLineCPM || strProjectCPM == strProjectCPMLeader) &&
            //    strProjectCPMLeader != strPMDirector)
            //{
            //    activityNameArr = new string[] {
            //            ActivityNameConsts.Submission,
            //            EZVisitActivityNameConsts.Product_Line_CPM,
            //            EZVisitActivityNameConsts.Project_CPM_Leader,
            //            EZVisitActivityNameConsts.PM_Director,
            //            ActivityNameConsts.End
            //        };
            //}
            //else
            //{
            //    if (strProjectCPMLeader != strProjectCPM && strProjectCPMLeader != strPMDirector)
            //    {
            //        activityNameArr = new string[] {
            //            ActivityNameConsts.Submission,
            //            EZVisitActivityNameConsts.Product_Line_CPM,
            //            EZVisitActivityNameConsts.Project_CPM,
            //            EZVisitActivityNameConsts.Project_CPM_Leader,
            //            EZVisitActivityNameConsts.PM_Director,
            //            ActivityNameConsts.End
            //        };
            //    }
            //    else
            //    {
            //        activityNameArr = new string[] {
            //            ActivityNameConsts.Submission,
            //            EZVisitActivityNameConsts.Product_Line_CPM,
            //            EZVisitActivityNameConsts.Project_CPM,
            //            EZVisitActivityNameConsts.PM_Director,
            //            ActivityNameConsts.End
            //        };
            //    }
            //}

            activityNameArr = new string[] {
                        ActivityNameConsts.Submission,
                        EZVisitActivityNameConsts.FNC_Controller,
                        EZVisitActivityNameConsts.Dept_Head,
                        EZVisitActivityNameConsts.COD_PP_Check_Person,
                        EZVisitActivityNameConsts.PDC,
                        EZVisitActivityNameConsts.COD_PP_Manager,
                        EZVisitActivityNameConsts.CEO_Delegation,
                        EZVisitActivityNameConsts.COD_PP_Head,
                        EZVisitActivityNameConsts.LGD_Check_Person,
                        EZVisitActivityNameConsts.LGD,
                        EZVisitActivityNameConsts.Post_Submission,
                        EZVisitActivityNameConsts.Post_COD_PP_Check_Person,
                        EZVisitActivityNameConsts.Post_Dept_Head,
                        EZVisitActivityNameConsts.Post_COD_PP_Manager,
                        EZVisitActivityNameConsts.Post_COD_PP_Head,
                       ActivityNameConsts.End
                                };


            ActivityLink activityLink = new ActivityLink(activityNameArr);
            return activityLink;
        }
    }
}
